/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.
 */

package jdbcmanager.app;

import java.io.*;

import javax.swing.*;

import jdbcmanager.swing.*;
import jdbcmanager.dbtools.dbconnect.*;

/**
 * "Data Base" tab of app config.
 * @author Francisco Morero Peyrona
 */
class ConfigTabData extends JPanel
{
   private JxSpinner spnLoginTimeOut;
   private JxSpinner spnQueryTimeOut;
   private JComboBox cmbUPC;
   private JxSpinner spnMaxRows;
   private JCheckBox chkUnlockTableEdit;
   
   //-------------------------------------------------------------------------//
   
   ConfigTabData()
   {
      init();
   }
   
   void save()
   {
      // Login time out 
      App.props().setSQLGridDefaultMaxRows( ((Integer) this.spnQueryTimeOut.getValue()).intValue() );
      
      // Query time out
      App.props().setSQLGridDefaultMaxRows( ((Integer) this.spnLoginTimeOut.getValue()).intValue() );
      
      // Start Connection
      String sStartUPC = (this.cmbUPC.getSelectedIndex() == 0) ? "" : this.cmbUPC.getSelectedItem() + App.sCONNECTION_FILE_EXT;
      App.props().setStartupUPC( sStartUPC );
      
      // Max Rows per table by default
      App.props().setSQLGridDefaultMaxRows( ((Integer) this.spnMaxRows.getValue()).intValue() );
      
      // Unlock table editing by default
      App.props().setSQLGridUnlocked( this.chkUnlockTableEdit.isSelected() );
   }
   
   private void init()
   {
      // Login time out ------------------------------------------
      int nLoginTime = App.props().getConnLoginTimeOut( 120 );
      this.spnLoginTimeOut = new JxSpinner( new SpinnerNumberModel( nLoginTime, 0, 3600, 1 ) );
      
      // Query time out ------------------------------------------
      int nQueryTime = App.props().getConnQueryTimeOut( 0 );   // It is not a must, but is a have to be 0
      this.spnQueryTimeOut = new JxSpinner( new SpinnerNumberModel( nQueryTime, 0, 3600, 1 ) );
      
      // Start-up connection -------------------------------------
      File[]   aFile = ConnectionDefinition.listFiles();
      String[] asUPC = new String[ aFile.length + 1 ];
               asUPC[0] = "None";
      
      for( int n = 0; n < aFile.length; n++ )
      {
         String sFile = aFile[n].getName();
         sFile = sFile.substring( 0, sFile.lastIndexOf( '.' ) ); // Removes file extension
         
         asUPC[n+1] = sFile;
      }
      
      String sUPC = App.props().getStartupUPC( "None" );
      
      if( sUPC.endsWith( App.sCONNECTION_FILE_EXT ) )
         sUPC = sUPC.substring( 0, sUPC.lastIndexOf( '.' ) ); // Removes file extension
      
      this.cmbUPC = new JComboBox( asUPC );
      this.cmbUPC.setSelectedItem( sUPC );
      
      // Maximun number of rows by default for tables ------------
      int nMaxRows = App.props().getSQLGridDefaultMaxRows( 250 );
      this.spnMaxRows = new JxSpinner( new SpinnerNumberModel( nMaxRows, 0, Integer.MAX_VALUE, 25 ) );
      
      // Unlock table eduting by default -------------------------
      this.chkUnlockTableEdit = new JCheckBox( "Allow table editing by default", App.props().getSQLGridUnlocked( false ) );
      
      // Add controls to this panel ----------------------------
      // To use another panel with BorderLayout and place all in NORTH is a trick to avoid using GridBagLayout()      
      JPanel pnl = new JPanel( new java.awt.GridLayout( 5,2,5,8 ) );
             pnl.setBorder( BorderFactory.createEmptyBorder( 9,9,9,9 ) );
      
      pnl.add( new JLabel( "Login time out (0 = no limit)" ) );
      pnl.add( this.spnLoginTimeOut );
      
      pnl.add( new JLabel( "Query time out (0 = no limit)" ) );
      pnl.add( this.spnQueryTimeOut );
      
      pnl.add( new JLabel( "Auto connect at start-up with" ) );
      pnl.add( this.cmbUPC ); 
      
      pnl.add( new JLabel( "Maximum rows by default" ) );
      pnl.add( this.spnMaxRows );
      
      pnl.add( this.chkUnlockTableEdit );
      pnl.add( new JLabel() );             // Trick to have pair number of components     
      
      setLayout( new java.awt.BorderLayout() );
      add( pnl, java.awt.BorderLayout.NORTH );
   }
}